﻿// JavaScript Document

var TimsCarList = {
    ShowPage: function (pageIndex, pageCount) {
        var self = this;
        var PageNum = Math.ceil(pageCount / MaxRows);
        var pageHtml = self.GetAjaxNavigate(pageIndex, PageNum, false);
        $(".page_list").html(pageHtml);
    },
    GetAjaxNavigate: function (selectedIndex, allpages, flag) {
        var tempNowPageIndex = "";
        //alert(selectedIndex);
        if (allpages < 2) return "";
        var innerhtml = "",
	        temppageIndex = '<li><a page="{0}" title="第{0}页" onclick="Holsun.Tims.VipTourCarList.prototype.SetPageIndex({0});Holsun.Tims.VipTourCarList.prototype.PageGetCarList();" class="J_4_a" href="javascript:;"><span>{0}</span></a></li>',
	        tempPassOver = '<li><span class="J_4_a"><span>..</span></span></li>',
	        tempNowPageIndex = '<li class="current"><a title="第{0}页" onclick="Holsun.Tims.VipTourCarList.prototype.SetPageIndex({0});Holsun.Tims.VipTourCarList.prototype.PageGetCarList();" class="J_4_a J_4_a1"><span>{0}</span></a></li>';
        if (selectedIndex == 1) {
            innerhtml += '<li><a title="第1页" class="J_4_a"><span>&lt;&lt;</span></a></li>';
        }
        else {
            innerhtml += '<li><a page="1" title="第1页" class="J_4_a" onclick="Holsun.Tims.VipTourCarList.prototype.SetPageIndex(1);Holsun.Tims.VipTourCarList.prototype.PageGetCarList();" href="javascript:;"><span>&lt;&lt;</span></a></li>';
        }
        if (selectedIndex > 1) {
            innerhtml += '<li><a page="' + (selectedIndex - 1) + '" onclick="Holsun.Tims.VipTourCarList.prototype.SetPageIndex(' + (selectedIndex - 1) + ');Holsun.Tims.VipTourCarList.prototype.PageGetCarList();"  title="上一页" class="J_4_a" href="javascript:;"><span>上一页</span></a></li>';
        }
        else {
            innerhtml += '<li><a title="上一页" class="J_4_a"><span>上一页</span></a></li>';
        }

        if (selectedIndex < 4 || allpages <= 5) {
            for (i = 1; i <= 5 && i <= allpages; i++) {
                if (selectedIndex == i) {
                    innerhtml += this.StringFormat(tempNowPageIndex, i);
                }
                else {
                    innerhtml += this.StringFormat(temppageIndex, i);
                }
            }
            if (allpages > 5) {
                innerhtml += tempPassOver;
            }
        }
        else {
            innerhtml += tempPassOver;
            if (selectedIndex + 1 > allpages && selectedIndex > 4)
                innerhtml += this.StringFormat(temppageIndex, (selectedIndex - 4));
            if (selectedIndex + 2 > allpages)
                innerhtml += this.StringFormat(temppageIndex, (selectedIndex - 3));
            if (flag) {
                if (selectedIndex < 10) {
                    innerhtml += this.StringFormat(temppageIndex, (selectedIndex - 2));
                }
            } else
                innerhtml += this.StringFormat(temppageIndex, (selectedIndex - 2));
            innerhtml += this.StringFormat(temppageIndex, (selectedIndex - 1));
            innerhtml += this.StringFormat(tempNowPageIndex, selectedIndex);
            if (selectedIndex + 1 <= allpages) {
                innerhtml += this.StringFormat(temppageIndex, (selectedIndex + 1));
            }
            if (selectedIndex + 2 <= allpages) {
                innerhtml += this.StringFormat(temppageIndex, (selectedIndex + 2));
            }
            if (selectedIndex + 3 <= allpages) {
                innerhtml += tempPassOver;
            }
        }

        if (allpages != selectedIndex) {
            innerhtml += '<li><a page="' + (selectedIndex + 1) + '" onclick="Holsun.Tims.VipTourCarList.prototype.SetPageIndex(' + (selectedIndex + 1) + ');Holsun.Tims.VipTourCarList.prototype.PageGetCarList();" title="下一页" class="J_4_a" href="javascript:;"><span>下一页</span></a></li>';
            innerhtml += '<li><a page="' + allpages + '" onclick="Holsun.Tims.VipTourCarList.prototype.SetPageIndex(' + allpages + ');Holsun.Tims.VipTourCarList.prototype.PageGetCarList();" title="最后一页" class="J_4_a" href="javascript:;"><span>&gt;&gt;</span></a></li>';
        }
        else {
            innerhtml += '<li><a title="下一页" class="J_4_a"><span>下一页</span></a></li>';
            innerhtml += '<li><a title="最后一页" class="J_4_a"><span>&gt;&gt;</span></a></li>';
        }
        innerhtml += '<li><span id="allpages">' + selectedIndex + '/' + allpages + '页</span></li>';
        //PageIndex=selectedIndex;
        return innerhtml;
    },
    SetPageIndex: function (pageIndex) {
        PageIndex = pageIndex;
    },
    StringFormat: function (result, args) {
        if (arguments.length > 0) {
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
            else {
                for (var i = 0; i < arguments.length; i++) {
                    if (arguments[i] == undefined) {
                        return "";
                    }
                    else {
                        var reg = new RegExp("({[" + i + "]})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
            return result;
        }
        else {
            return this;
        }
    }
};

$(document).ready(function () {
    $("#uiPager").click(function () { PageIndex = PageIndex + 1; TimsCarList.PageGetCarList(); });
  TimsCarList.PageGetCarList();
  $("#city_select").change(function(){TimsCarList.CitySelectEven(this);});
  $("#cartype_select").change(function(){TimsCarList.CarTypeSelectEven(this);});
  $("#brand_select").change(function(){TimsCarList.BrandSelectEven(this);});
});